package com.zwz.crawler;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import java.io.IOException;

@Slf4j
public class TungeeEnterpriseGraph {
    public static void startTungeeEnterpriseGraph( ) {
        // 构建带参数的URL（GET参数直接拼接）
        String entityId = "577de19eadf21dc4";
        String url = "https://sales.tungee.com/api/graph/enterprise-graph"
                + "?entity_id=" + entityId
                + "&entity_type=enterprise";

        try {
            Connection.Response response = Jsoup.connect(url)
                    .method(Connection.Method.GET)
                    // 设置关键请求头（按浏览器格式完整复制）
                    .header("authority", "sales.tungee.com")
                    .header("scheme", "https")
                    .header("accept", "*/*")
                    .header("accept-encoding", "gzip, deflate, br, zstd")
                    .header("accept-language", "zh-CN,zh;q=0.9")
                    .header("cache-control", "no-cache")
                    .header("pragma", "no-cache")
                    .header("priority", "u=1, i")
                    .header("referer", "https://sales.tungee.com/enterprise-details/577de19eadf21dc4/enterprise-map")
                    .header("sec-ch-ua", "\"Not;A=Brand\";v=\"24\", \"Chromium\";v=\"128\"")
                    .header("sec-ch-ua-mobile", "?0")
                    .header("sec-ch-ua-platform", "\"Windows\"")
                    .header("sec-fetch-dest", "empty")
                    .header("sec-fetch-mode", "cors")
                    .header("sec-fetch-site", "same-origin")
                    .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36")
                    .header("x-tonxis-pid", "67864ea75f69e05d3b544ca7")
                    // 设置完整 Cookie（需替换为最新值）
                    .header("cookie", "_tx_pid=67864ea75f69e05d3b544ca7; __wpkreporterwid_=cc4a07fc-7bb9-40a4-96bc-2cbac34b658f; __last_enter_version=sales; _co_i=6805a5576eceb9637696f09d; _tx_uid=680701dddca941522b77e864; _tx_cid=6805a5576eceb9637696f09d; DISTINCT_ID=b84fe276-099f-4932-92b3-d54b92ae384b; _tx_sid=MTc0NTMwMTE0MzYyMjo4MmQ2OTQ5MTY0NjU2ODNmYjQ5MjlmMjc3NDg5ZDFlZg==; SecurityCenterDuId=ImFBSG15RzdPdVdPdCtsWnNGMFVuSXhVPSI.GujGMQ.drG94beetGxPTF2QwZEbncLE74I; remember_token=680734b1dca9415227c304ff|c3627fddf0071ffcba037dc9a78db2751dfcbda40933b7b1a5c8b4931dffe8288c043aaa80501e311535299f4fe7aa79cc677163a303cfef2f3f7e0d612681cb; Hm_lvt_f2ee75449fc055cc4dbceb4fe403bea3=1745301094,1745302709; HMACCOUNT=B4E402DF057C895E; tg_referrer_source=https%3A%2F%2Fcrm.tungee.com%2F; acw_tc=1a0c399617453086067532341e005da98dbe9cfa91570b0cd4ff7b905fba9c; Hm_lpvt_f2ee75449fc055cc4dbceb4fe403bea3=1745308697; accountCenterSessionId=.eJwdzkFqw0AMheG7zDoLaSR7JrlAKdQpBUOwN0Yz0iTBSQq200JK714368f_-H7cfbZpOKvbuTpCIE6oWbaMlfchE3ApbuOGMtl8crtlutvG5ZPcbnZZk29L_-szJ6FYImICI61JC5ZYo6FaslQCBiOJ2ScFFjbmnHMIQgAeJMMWAEKJpDGyCCOFRFKkyoF9roAkKVegmGrgCL5mVDYPvhitzxrIVsjNTIdZvmxYPgdNblfkMq_g-Qlsrq-4f_mouse49O3Rd2eAph3xrT1Sd2iWrh2pv_bX_aPB90NH7vcPgdtUig.GujeDw.DsF49_g4SaUd0IJj4nudU6AlQJQ; doncusSessionId=eyJfZnJlc2giOmZhbHNlLCJ1c2VyX2lkIjoiNjgwNzAxZGRkY2E5NDE1MjJiNzdlODY0In0.GujeK")
                    .ignoreContentType(true)
                    .ignoreHttpErrors(true)
                    .maxBodySize(0) // 禁用默认响应大小限制
                    .execute();

            // 处理响应
           log.info("HTTP 状态码: " + response.statusCode());
//           //log.info("响应内容:\n" + response.body());
            // 错误处理逻辑
            if (response.statusCode() == 401) {
                handleUnauthorizedError();
            } else if (response.statusCode() == 403) {
                System.err.println("权限不足，请检查 enterprise_id 归属");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void handleUnauthorizedError() {
        System.err.println("授权失败可能原因：");
        System.err.println("1. Cookie 已过期（最新 Cookie 需通过浏览器登录获取）");
        System.err.println("2. 需要动态生成 x-tonxis-signature（未在请求头中发现该参数）");
        System.err.println("3. 接口访问频率触发风控限制");
    }
}